﻿using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class MailConfigController : BaseAuthController
    {
        /// <summary>
        /// 返回Index视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        public JsonResult GetList(string page, string rows)
        {
            MailConfigBLL bll = new MailConfigBLL();
            PagedList<EMOA_MailConfig> list = bll.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows));
            return ToJson<EMOA_MailConfig, MailConfigModels>(list, list.TotalItemCount);
        }

        /// <summary>
        /// 增加实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(MailConfigModels model)
        {
            try
            {
                MailConfigBLL bll = new MailConfigBLL();
                EMOA_MailConfig entity = model.ToEntity<EMOA_MailConfig>();
                entity.MailConfigID = Guid.NewGuid();
                entity.OwnerUserID = CurrentUser.UserID;
                entity.CreateBy = CurrentUser.UserID;
                entity.CreateTime = DateTime.Now;
                if (bll.Add(entity))
                {
                    return Content("OK");
                }
                else
                {
                    return Content("增加失败");
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(MailConfigModels model)
        {
            try
            {
                MailConfigBLL bll = new MailConfigBLL();
                EMOA_MailConfig entity = bll.GetEntity(model.MailConfigID);
                entity = model.ToEntity<EMOA_MailConfig>(entity);
                entity.UpdateBy = CurrentUser.UserID;
                entity.UpdateTime = DateTime.Now;
                if (bll.Update(entity))
                    return Content("OK");
                else
                    return Content("修改失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                MailConfigBLL bll = new MailConfigBLL();
                if (bll.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("删除失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

    }
}